<?php include 'config.php'?>
<?php
session_start();
$comp_id = $_SESSION['comp_id'];

$conn = mysql_connect($host, $user, $password) or die(mysql_error());
mysql_select_db("mweb_survey");

$sql_comp = "SELECT * FROM company WHERE CompID='$comp_id'";
$result_comp = mysql_query($sql_comp, $conn) or die(mysql_error());
$row_comp = mysql_fetch_assoc($result_comp);
$comp_name = $row_comp['CompName'];

$sql_grp = "SELECT * FROM ques_grp";
$result_grp = mysql_query($sql_grp, $conn) or die(mysql_error());

putenv("TZ=US/Eastern");
$datetime = date("Y-m-d H:i:s");

while ($row_grp = mysql_fetch_assoc($result_grp)) {
	$grp_id = $row_grp['QuesGrpID'];
	
	$sql_ques = "SELECT * FROM questions WHERE QuesGrpID='$grp_id'";
	$result_ques = mysql_query($sql_ques, $conn) or die(mysql_error());
	
	while ($row_ques = mysql_fetch_assoc($result_ques)) {
		$ques_id = $row_ques['QuesID'];
		$ques_type = $row_ques['QuesType'];
		
		if ($ques_type == 'Textfield') {
			if (isset($_POST["textfield$grp_id$ques_id"])) {
				$ans = $_POST["textfield$grp_id$ques_id"];
			} else {
				$ans = "Not Avail";
			}
			$sql = "INSERT INTO answers VALUES ('$comp_id', '$ques_id', '$ques_type', '$datetime', '$ans')";
			mysql_query($sql, $conn) or die(mysql_error());
		}
		if ($ques_type == 'Radio') {
			if (isset($_POST["radio$grp_id$ques_id"])) {
				$ans = $_POST["radio$grp_id$ques_id"];
			} else {
				$ans = "Not Avail";
			}
			$sql = "INSERT INTO answers VALUES ('$comp_id', '$ques_id', '$ques_type', '$datetime', '$ans')";
			mysql_query($sql, $conn) or die(mysql_error());
		}
		
		if ($ques_type == 'Checkbox') {
			if (isset($_POST["checkbox$grp_id$ques_id"])) {
				$ans_array = $_POST["checkbox$grp_id$ques_id"];
				$n = count($ans_array);
				for ($i = 0; $i < $n; $i++) {
					$ans = $ans_array[$i];
					//echo $ans;
					$sql = "INSERT INTO answers VALUES ('$comp_id', '$ques_id', '$ques_type', '$datetime', '$ans')";
					mysql_query($sql, $conn) or die(mysql_error());
				}
			} else {
				$ans = "Not Avail";
				$sql = "INSERT INTO answers VALUES ('$comp_id', '$ques_id', '$ques_type', '$datetime', '$ans')";
				mysql_query($sql, $conn) or die(mysql_error());
			}
		}
		
		if ($ques_type == 'Dropdown') {
			if (isset($_POST["dropdown$grp_id$ques_id"])) {
				$ans = $_POST["dropdown$grp_id$ques_id"];
			} else {
				$ans = "Not Avail";
			}
			$sql = "INSERT INTO answers VALUES ('$comp_id', '$ques_id', '$ques_type', '$datetime', '$ans')";
			mysql_query($sql, $conn) or die(mysql_error());
		}
	}
}

if (isset($_POST["comment"])) {
	$ans = $_POST["comment"];
} else {
	$ans = "Not Avail";
}
$sql = "INSERT INTO answers VALUES ('$comp_id', '2600', '$ques_type', '$datetime', '$ans')";
mysql_query($sql, $conn) or die(mysql_error());

?>
<html>
<head>
	<title>Survey</title>
	<meta charset="utf-8" />
	<meta name="viewport" content="width=device-width; initial-scale=1.0; maximum-scale=1.0;" />
	<meta name="format-detection" content="telephone=no" />
	
	<link rel="stylesheet" href="css/style.css" />
</head>
<body>
<?php
echo "Dear $comp_name,<br/><br/>";
echo "Your survey input has been submitted successfully.<br/><br/>";
echo "Thank you!";
?>
<?php include 'footer.php';?>
</body>
</html>